function init_calendar () {
    var date = Calendar.setup({
        inputField: 'dob',
        trigger: 'dob-date',
        dateFormat: '%Y/%m/%d', align: 'Br', showTime: false, minuteStep: 1,
        onSelect: function(date) {
            var dt = date.selection.print('%a, %e %B %Y');
            if (dt.length > 0) {
                Ext.fly('dob-date').dom.value = dt.join('');
            }
            date.hide();
        }
    });
} // init_calendar

function save () {
    var proceed = true;
    var transId = '';
    var focuson = '';
    var message = Ext.get('message');

    message.setStyle({ display:'none' });
    Ext.select('.invalid', false, 'form-account').removeClass('invalid');
    Ext.select('div.invalidField', false, 'form-account').remove();
    Ext.select('td.invalidText', false, 'form-account').removeClass('invalidText');
    Ext.each(Ext.select('.req', false, 'form-account').elements, function(e){
        var el = Ext.get(e);

        if (!el.getValue() || el.getValue() == 'select..') {
            proceed = false;
            el.addClass('invalid');
            el.up('td').prev('td').addClass('invalidText');
            if (!focuson) {
                focuson = e.id;
                el.focus();
                el.fadeIn({
                    duration: 2.0
                });
            }
        }
    });

    if (!proceed) {
        return false;
    }

    transId = Ext.Ajax.request({
        form: 'form-account',
        success: function(response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.status == false) {
                if (obj.mesg) {
                    message.update(obj.mesg);
                    message.addClass('error').highlight();
                }
                if (Ext.isArray(obj.error)) {
                    Ext.each(obj.error, function(e) {
                        var el  = Ext.get(e.id);
                        var err = '<div class="invalidField" style="display:none;">'+e.code+'</div>';
                        el.addClass('invalid');
                        el.up('td').prev('td').addClass('invalidText');
                        el.insertHtml('afterEnd', err);
                        el.next('div.invalidField').slideIn('t', {
                            duration:.4
                        });
                        if (!focuson) {
                            focuson = e.id;
                            el.focus();
                            el.fadeIn({
                                duration: 2.0
                            });
                        }
                    });
                }
            } else {
                if (obj.mesg) {
                    message.update(obj.mesg);
                    message.removeClass('error').addClass('success').highlight();
                    message.pause(8).fadeOut({ useDisplay:true });
                }
            }
        },
        failure: function(response, opts) {
            console.log('server-side failure with status code '+response.status);
        },
        callback: function() {
            Ext.fly('progress').ghost('b', { useDisplay:true });
            Ext.each(Ext.query('input[type=submit],input[type=button]', 'form-account'), function(e){ e.disabled = false; });
        }
    });

    if (Ext.Ajax.isLoading(transId)) {
        Ext.each(Ext.query('input[type=submit],input[type=button]', 'form-account'), function(e){ e.disabled = true; });
        Ext.fly('progress').update('Saving..');
        Ext.fly('progress').fadeIn();
    }

    return false;
} // save

function message (msg, focus) {
    alert(msg);

    if (typeof focus != 'undefined') {
        Ext.fly(focus).focus();
    }
} // message

function init (){
    init_calendar();
}// init